package com.example.demo.utils;

import com.example.demo.service.AuthService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lucy
 * @create 2024-03-20 15:19
 */
public class FaceMatchUtil {
    public static String faceMatch(String file1, String file2) {

        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        try {

            List<Map<String, Object>> jsonArray = new ArrayList<>();

            Map<String, Object> jsonObject1 = new HashMap<>();
            jsonObject1.put("image", file1);
            jsonObject1.put("image_type", "BASE64");
            jsonObject1.put("face_type", "LIVE");
            jsonObject1.put("quality_control", "LOW");
            jsonObject1.put("liveness_control", "HIGH");
            jsonArray.add(jsonObject1);

            // 第二个JSON对象
            Map<String, Object> jsonObject2 = new HashMap<>();
            jsonObject2.put("image", file2);
            jsonObject2.put("image_type", "BASE64");
            jsonObject2.put("face_type", "LIVE");
            jsonObject2.put("quality_control", "LOW");
            jsonObject2.put("liveness_control", "HIGH");
            jsonArray.add(jsonObject2);

            String param = GsonUtil.toJson(jsonArray);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken =  AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
